package com.qf.sl.youshare;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.*;
import android.widget.*;
import com.qf.sl.youshare.adapters.CommonImageAdapter;
import com.qf.sl.youshare.adapters.FunctionAdapter;
import com.qf.sl.youshare.transform.DepthPageTransformer;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class MainActivity
        extends FragmentActivity
        implements ViewPager.OnPageChangeListener,
                     AdapterView.OnItemClickListener {

    private LinkedList<Map<String, ?>> data;
    private LinkedList<Integer> imageIds;
    private ActionBar actionBar;
    private ViewPager pager;
    private GridView gridView;
    private RadioGroup radioGroup;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //去掉 ActionBar 的图标,标题居中

        actionBar = getActionBar();

        pager = (ViewPager) findViewById(R.id.main_poster);

        //TODO 九宫格
        gridView = (GridView) findViewById(R.id.main_entry);

        radioGroup = (RadioGroup) findViewById(R.id.radio_group_chose);

        init();
    }

    /**
     * 初始化数据
     */
    private void init() {

        if (actionBar != null) {
            //设置透明图,去掉图标
            actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

            //ActionBar 标题居中
            //设置一个默认的标题
            actionBar.setDisplayShowCustomEnabled(true);

            LayoutInflater inflater = LayoutInflater.from(this);

            View view = inflater.inflate(R.layout.actionbar_main_title, null);

            TextView txtTitle = (TextView) view.findViewById(R.id.actionbar_title);

            if (txtTitle != null) {
                txtTitle.setText("优享");
            }

            actionBar.setCustomView(
                    view,
                    new ActionBar.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            Gravity.CENTER
                    )
                );

            actionBar.setDisplayShowTitleEnabled(false);

        }

        if (pager != null) {
            //TODO 设置 Adapter
            imageIds = new LinkedList<Integer>();

            initViewPagerListData();

            CommonImageAdapter adapter = new CommonImageAdapter(imageIds, this);
            pager.setAdapter(adapter);
            pager.setPageTransformer(true,new DepthPageTransformer());

            pager.setOnPageChangeListener(this);
        }

        if (gridView != null) {
            data = new LinkedList<Map<String, ?>>();

            initGridListData();

//            gridView.setAdapter(new SimpleAdapter(
//                    this,
//                    data,
//                    R.layout.item_main_enter_point,
//                    new String[]{"bg", "icon", "title"},
//                    new int[]{R.id.item_enter_point_bg, R.id.item_enter_point_icon, R.id.item_enter_point_title}
//            ));

            FunctionAdapter adapter = new FunctionAdapter(this, data);

            gridView.setAdapter(adapter);

            gridView.setOnItemClickListener(this);

        }

        if (radioGroup != null) {
            RadioButton childAt = (RadioButton) radioGroup.getChildAt(0);
            childAt.setChecked(true);
//            radioGroup.setOnCheckedChangeListener(this);
        }
    }

    /**
     * 初始化ViewPager中的数据
     */
    private void initViewPagerListData() {
        imageIds.add(R.drawable.image1);
        imageIds.add(R.drawable.image2);
        imageIds.add(R.drawable.image3);
        imageIds.add(R.drawable.image4);
        imageIds.add(R.drawable.image5);
    }

    /**
     * 初始化GridView中的List中的数据
     */
    private void initGridListData() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("bg", R.drawable.ico_yingshi_bg);
        map.put("icon", R.drawable.ico_yingshi);
        map.put("title",getString(R.string.main_video));
        data.add(map);

        map = new HashMap<String, Object>();
        map.put("bg",R.drawable.ico_shangcheng_bg);
        map.put("icon", R.drawable.ico_shangcheng);
        map.put("title",getString(R.string.main_store));
        data.add(map);

        map = new HashMap<String, Object>();
        map.put("bg",R.drawable.ico_xianchang_bg);
        map.put("icon", R.drawable.ico_xianchang);
        map.put("title",getString(R.string.main_spot));
        data.add(map);

        map = new HashMap<String, Object>();
        map.put("bg",R.drawable.ico_shejiao_bg);
        map.put("icon", R.drawable.ico_shejiao);
        map.put("title",getString(R.string.main_social));
        data.add(map);

        map = new HashMap<String, Object>();
        map.put("bg",R.drawable.ico_user_bg);
        map.put("icon", R.drawable.ico_user);
        map.put("title",getString(R.string.main_personal));
        data.add(map);

        map = new HashMap<String, Object>();
        map.put("bg",R.drawable.ico_setting_bg);
        map.put("icon",R.drawable.ico_setting);
        map.put("title",getString(R.string.main_setting));
        data.add(map);
    }

    /**
     * 加载菜单
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    /**
     * actionBar中菜单键的监听
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int featureId = item.getItemId();

        switch (featureId){
            case R.id.action_register://点击注册菜单
                Intent registerIntent = new Intent(this, UserRegisterActivity.class);
                startActivity(registerIntent);
                break;
            case R.id.action_login:
                Intent loginIntent = new Intent(this, LoginActivity.class);
                startActivity(loginIntent);
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 上一次点击Back按键的时间
     */
    private long lastBackTime = 0;

    /**
     * 当前 Activity 显示的时候,点击屏幕上的返回键的事件监听处理方法
     */
    @Override
    public void onBackPressed() {

        //1,获取当前的毫秒数
        long currentTime = System.currentTimeMillis();
        //2,和上一次点击返回键的时间进行比较,相差多少秒就退出

        if (currentTime - lastBackTime <= 2000){
            //TODO 结束程序
            //利用 super.onBackPressed() 让 Activity 自己处理返回键
            //或者利用 finish() 结束

            finish();

            //也可以通过 System.exit(0) 这个方法,终止整个应用程序进程
            //进程终止,类似 Service 之类的组件将不再继续运行

        }else {
            lastBackTime = currentTime;
            Toast.makeText(this,"再次点击退出",Toast.LENGTH_SHORT).show();
        }

    }

    ////////////////// ViewPager 切换的事件监听 //////////////////

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        View v = radioGroup.getChildAt(position);
        if (v != null) {
            if (v instanceof RadioButton){
                RadioButton rb = (RadioButton) v;
                rb.setChecked(true);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * GridView 的监听事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = null;

        switch (position){
            case 0:
                break;
            case 1://跳转商城
                intent = new Intent(this,StoreActivity.class);
                break;
            case 2:
                break;
            case 3://跳转社交
                intent = new Intent(this, SheJiaoActivity.class);
                break;
            case 4:
                break;
            case 5://跳转设置
                intent = new Intent(this, SettingActivity.class);
                break;
            default:
                break;
        }
        if (intent != null) {
            startActivity(intent);
        }

    }

}
